/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.synonymousEngine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

/**
 * @author grupo JFTS
 *
 */
public class SpanishSynomEngine implements ISynonymEngine {
		private RAMDirectory directory = null;
		private IndexSearcher searcher = null; 
		private Map<String, String> args = null;
		
		public SpanishSynomEngine(Map<String, String> param)  throws IOException {
			this.args = param;
			directory = new RAMDirectory(FSDirectory.getDirectory(args.get("indexPath")));
			searcher = new IndexSearcher(directory);				
		}
		
	@Override
	public String[] getSynonyms(String s) throws IOException {
		ArrayList<String> synList = new ArrayList<String>();
		
		int linesResultProcess = Integer.parseInt(args.get("linesResultProcess"));
		int synInject = Integer.parseInt(args.get("synInject"));
		
		TopDocs results = searcher.search(new TermQuery(new Term("word", s)), linesResultProcess);
		ScoreDoc[] hits = results.scoreDocs;
		int end = 0;
        for (ScoreDoc hit : hits) {
            Document doc = searcher.doc(hit.doc);
			String[] values = doc.getValues("syn");
			end = (values.length > synInject) ? synInject : values.length;
			for (int j = 0; j < end; j++) {
				synList.add(values[j]);
			}
        }		
		return (String[]) synList.toArray(new String[0]);
	}
}